package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.utils.AESUtil;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class InstrallPayPsdTwoActivity extends BaseActivity implements View.OnClickListener{

    private ImageView gpsBack;
    private ImageView paypsdback;
    private EditText fristpsd;
    private EditText secondpsd;
    private Button paypsdsure;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String word;
    private String messageId;
    private String city;
    private String fruit;
    private String school;
    private String code;
    private String from;
    private TextView intalltitle;
    private String url;
    private AlertDialog dialog;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sou_suo_gsp);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        Intent intent = getIntent();
        messageId = intent.getStringExtra("messageId");
        city = intent.getStringExtra("city");
        fruit = intent.getStringExtra("fruit");
        school = intent.getStringExtra("school");
        code = intent.getStringExtra("code");
        from = intent.getStringExtra("from");
        url = intent.getStringExtra("url");
        initPayPsdView();
        if (from.equals("install")){
            intalltitle.setText("设置支付密码");
        }else {
            intalltitle.setText("修改支付密码");
        }
    }

    private void initPayPsdView() {
        paypsdback = (ImageView) findViewById(R.id.paypsdtwo_back);
        fristpsd = (EditText) findViewById(R.id.paypsd_one);
        secondpsd = (EditText) findViewById(R.id.paypsd_two);
        paypsdsure = (Button) findViewById(R.id.paypsdtwo_sure);
        intalltitle = (TextView) findViewById(R.id.installpsdtitle);
        backmain = (ImageView) findViewById(R.id.instrallpaypsg_backmain);
        backmain.setOnClickListener(this);
        paypsdback.setOnClickListener(this);
        paypsdsure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.instrallpaypsg_backmain:
                startActivity(new Intent(InstrallPayPsdTwoActivity.this,MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                finish();
            case R.id.paypsdtwo_back:
                finish();
                break;
            case R.id.paypsdtwo_sure:
                String fristword = String.valueOf(fristpsd.getText());
                String secondword = String.valueOf(secondpsd.getText());
               if (TextUtils.isEmpty(fristword)){
                   Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
               }else if (TextUtils.isEmpty(secondword)){
                   Toast.makeText(this, "请确认密码", Toast.LENGTH_SHORT).show();
               }else {

                   AlertDialog.Builder builder = new AlertDialog.Builder(InstrallPayPsdTwoActivity.this, R.style.NoBackGroundDialog);
                   View view = LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
                   builder.setView(view);
                   dialog = builder.create();
                   dialog.show();

                   installPayWord();
               }
                break;
        }
    }
    private void installPayWord() {
        String payword = String.valueOf(secondpsd.getText());
        try {
            word = AESUtil.encryptAES(payword);
        } catch (Exception e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams(InternetAddressUtis.Url+url);// 验证码接口
        params.addHeader("Content-Type","application/json");
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        params.addHeader("token", token);
        JSONObject object = new JSONObject();
        object.put("payPassword", word);
        object.put("messageId", messageId);
        object.put("city",city);
        object.put("fruit",fruit);
        object.put("code",code);
        object.put("school",school);
        Log.d("tag", "installPayWord: 支付密码--"+appId+"//"+userId+"//"+token+"//"+word+"//"+messageId+"//"+city+"//"+fruit+"//"+school+"//"+code);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                dialog.dismiss();
                Log.d("tag", "onSuccess: 设置支付密码--"+result);
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    Toast.makeText(InstrallPayPsdTwoActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    String failure = obj.getString("failure");
                    if (failure.equals("1005")){
                        Toast.makeText(InstrallPayPsdTwoActivity.this, "短信验证码错误，请重新获取", Toast.LENGTH_SHORT).show();
                    }else if (failure.equals("1059")){
                        Toast.makeText(InstrallPayPsdTwoActivity.this, "密保问题答案错误，请重新输入", Toast.LENGTH_SHORT).show();
                    }else {}
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                dialog.dismiss();
                Log.d("tag", "onError: 设置支付密码--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
